package com.manli.api.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.manli.api.base.exception.MyException;
import com.manli.api.pojo.dingdanxia.*;
import com.manli.common.utils.HttpUtil;
import com.manli.common.utils.StringUtil;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DingDanXiaUtils {

    /**
     * 淘宝客高佣转链api（万能版）
     * @return
     */
    public TklToHelpersReturn tklToHelpers(TklToHelpersQuery tklToHelpersQuery)throws Exception{
        Map map = BeanMap.create(tklToHelpersQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.anyToHelpers,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            TklToHelpersReturn tklToHelpersReturn = JSONObject.parseObject(resultJson.getJSONObject("data").toJSONString(),TklToHelpersReturn.class);
            return tklToHelpersReturn;
        }

        return null;
    }

    /**
     * 淘宝客短链转长链
     * @return
     */
    public String tklShortToLongUrl(Map map)throws Exception{
        String result = HttpUtil.httpPost(DingDanXiaUrls.shortToLong,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            String url = resultJson.getJSONObject("data").getString("url");
            //删除url中的pid
            url = StringUtil.deleteUrlParam(url,"pid");
            return url;
        }

        return null;
    }

    /**
     * 拼多多超级搜索接口,关键词/商品ID查询优惠券列表接口
     * @return
     */
    public PddToHelpersReturn pddToHelpers(PddToHelpersQuery pddToHelpersQuery)throws Exception{
        Map map = BeanMap.create(pddToHelpersQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.pddToHelpers,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            if(resultJson.getInteger("total_results").intValue() > 0){
                JSONArray array = resultJson.getJSONArray("data");
                PddToHelpersReturn pddToHelpersReturn = JSONObject.parseObject(array.get(0).toString(),PddToHelpersReturn.class);
                return pddToHelpersReturn;
            }else{
                return null;
            }
        }

        return null;
    }

    /**
     * 拼多多商品转链api接口(商品id版)
     * @return
     */
    public PddToUrlHelpersReturn pddToUrlHelpers(PddToUrlHelpersQuery pddToHelpersQuery)throws Exception{
        Map map = BeanMap.create(pddToHelpersQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.pddToUrlHelpers,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            PddToUrlHelpersReturn tklToHelpersReturn = JSONObject.parseObject(resultJson.getJSONObject("data").toJSONString(),PddToUrlHelpersReturn.class);
            return tklToHelpersReturn;
        }

        return null;
    }

    /**
     * 商品URL链接/关键词/类目/商品ID查询优惠券列表接口,京东超级搜索接口
     * @return
     */
    public JdToHelpersReturn jdToHelpers(JdToHelpersQuery jdToHelpersQuery)throws Exception{
        Map map = BeanMap.create(jdToHelpersQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.jdToHelpers,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            if(resultJson.getInteger("total_results").intValue() > 0){
                JSONArray array = resultJson.getJSONArray("data");
                JdToHelpersReturn jdToHelpersReturn = JSONObject.parseObject(array.get(0).toString(),JdToHelpersReturn.class);
                return jdToHelpersReturn;
            }else{
                return null;
            }
        }

        return null;
    }

    /**
     * 京东商品转链api接口(商品id版)
     * @return
     */
    public JdToUrlHelpersReturn jdToUrlHelpers(JdToUrlHelpersQuery jdToUrlHelpersQuery)throws Exception{
        Map map = BeanMap.create(jdToUrlHelpersQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.jdToUrlHelpers,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            JdToUrlHelpersReturn tklToHelpersReturn = JSONObject.parseObject(resultJson.getJSONObject("data").toJSONString(),JdToUrlHelpersReturn.class);
            return tklToHelpersReturn;
        }

        return null;
    }


    /**
     * 淘宝客高佣转链api（万能版）
     * @return
     */
    public JSONArray taobaokeSuperSearch(JdToHelpersSuperQuery jdToHelpersSuperQuery)throws Exception{
        Map map = BeanMap.create(jdToHelpersSuperQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.taobaokeSuperSearch,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            JSONArray data = resultJson.getJSONArray("data");
            return data;
        }else{
            throw new MyException(-1,"" + resultJson.getString("msg"));
        }
    }

    /**
     * 淘宝客商品多级分类-带图标api接口
     * @return
     */
    public JSONArray taobaokeGoodsClass(TaobaoGoodsClassQuery taobaoGoodsClassQuery)throws Exception{
        Map map = BeanMap.create(taobaoGoodsClassQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.taobaokeGoodsClass,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            JSONArray data = resultJson.getJSONArray("data");
            return data;
        }else{
            throw new MyException(-1,"" + resultJson.getString("msg"));
        }
    }

    /**
     * 淘宝客商品优惠券列表页API接口
     * @return
     */
    public JSONObject taobaokeGoodsCouponList(TaobaoGoodsCouponListQuery taobaoGoodsClassQuery)throws Exception{
        Map map = BeanMap.create(taobaoGoodsClassQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.taobaokeGoodsCouponList,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            return resultJson;
        }else{
            return null;
        }
    }

    /**
     * 淘宝客商品优惠券列表页API接口，直接查询商品id
     * @return
     */
    public JSONObject taobaokeGoodsCouponInfo(TaobaoGoodsCouponListQuery taobaoGoodsClassQuery)throws Exception{
        Map map = BeanMap.create(taobaoGoodsClassQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.taobaokeGoodsCouponList,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            JSONObject data = resultJson.getJSONObject("data");
            return data;
        }else{
            throw new MyException(-1,"" + resultJson.getString("msg"));
        }
    }

    /**
     * 淘宝客高佣转链api（商品id版）
     * @return
     */
    public JSONObject taobaokeGoodsToTklById(TaobaoGoodsToTklByGoodsIdQuery taobaoGoodsClassQuery)throws Exception{
        Map map = BeanMap.create(taobaoGoodsClassQuery);
        String result = HttpUtil.httpPost(DingDanXiaUrls.taobaokeGoodsToTklById,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            JSONObject data = resultJson.getJSONObject("data");
            return data;
        }else{
            throw new MyException(-1,"" + resultJson.getString("msg"));
        }
    }
}
